package com.web.servlet.dao.impl;


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

import com.web.servlet.bean.Player;
import com.web.servlet.dao.DBConnect;
import com.web.servlet.dao.DBPoolManager;
import com.web.servlet.dao.StoredProcedureDao;
import com.web.servlet.db.StrategyBaseDao;
import com.web.servlet.utils.GenerateNum;
import com.web.servlet.utils.Utils;

public class StoredProcedureDaoImpl extends StrategyBaseDao implements StoredProcedureDao {

	@Override
	public Player loginByUserName(String userName, String password, String strIp) {
		{
			Player player =null;
			try
			{
	            Connection con = DBConnect.connDataBase();
	            
	            String currTime =Utils.getCurrTime();
	    
	            CallableStatement statement =  con.prepareCall("{call pro_UserLogin(?,?,?,?)}");
		    	statement.setString(1, userName);
	    		statement.setString(2, password);
	    		statement.setString(3, strIp);
	    		statement.setString(4, currTime);
	    		ResultSet rs= statement.executeQuery();
	        
	            while(rs.next())
	            {
	            	player =new Player();
	            	player.setId(rs.getInt("userId"));
	            	player.setName(rs.getString("userName"));
	            	player.setNickName(rs.getString("userNikeName"));
	            	player.setCoin(rs.getInt("userCoin"));
	            	player.setHeadImgIndex(rs.getInt("headImgIndex"));
	            	player.setSex(rs.getInt("userSex"));
	            	player.setSex(rs.getInt("userDiamond"));
	            	player.setSzWord(rs.getString("userWord"));
	            	break;
	            }
	            DBPoolManager.closeConn(con);
	            if (rs!=null)
	         	   rs.close();
	            if (con!=null)
	         	   con.close();
	            if (statement!=null)
	         	   statement.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
			return player;
		}
	}

	@Override
	public int addUser(String userName, String password, String userNikeName,
			String strIp) {
		String currTime =Utils.getCurrTime();
		int outReturnValue = 0; 
		try
		{
            Connection con = null;
            CallableStatement statement = null;
            con = DBConnect.connDataBase();
            while(true)
            {
            	//随机产生用户Id
                String randomUserId = GenerateNum.generateNumber5_9();
	            statement =  con.prepareCall("{call pro_UserRegister(?,?,?,?,?,?,?)}");
	    		statement.setLong(1, Long.valueOf(randomUserId));
	    		statement.setString(2, userName);
	    		statement.setString(3, password);
	    		statement.setString(4, userNikeName);
	    		statement.setString(5, currTime);
	    		statement.setString(6, strIp);
	    		statement.registerOutParameter(7, java.sql.Types.INTEGER);
	    		ResultSet res= statement.executeQuery();
	    		outReturnValue = statement.getInt(7);
	    		if (outReturnValue==0)
	    		{
	    			break;
	    		}
	    		if(outReturnValue==2)
	    		{	
	    			break;
	    		}
            }
            statement.close();
            DBPoolManager.closeConn(con);
            if (con!=null)
	        	   con.close();
            
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
		
		if (outReturnValue==0)
		{
			return 0;
		}
		if(outReturnValue==2)
		{	
			return 0;
		}
		
		return 0;
	}
	
}
